package com.amikomgamedev.ramashinta.state;

import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;

import android.view.KeyEvent;

import com.amikomgamedev.ramashinta.Menu_Scroll;
import com.amikomgamedev.ramashinta.Utils;

public class State_Menu_Select_Stage extends State_Menu_Level
{

	private Menu_Scroll scroll;
	
	protected void create_Menu_Select_Stage() 
	{
		graphic.attachStage();
		
		scroll = new Menu_Scroll(activity, (Utils.getRatioH(RES_WIDTH - Utils.getRatioW(300)) / 2), graphic.spr_Icon_Stage[0], graphic.spr_Icon_Stage[1],
				graphic.spr_Icon_Stage[2],graphic.spr_Icon_Stage[3],graphic.spr_Icon_Stage[4]);
	}
	
	@Override
	public void onUpdate(float pSecondsElapsed) {
		// TODO Auto-generated method stub
		if(State_Menu_Current == STATE_MENU_STAGE)
		{
			for(int i=0; i < 5; i++)
			{
				if(scroll.getFocus() == i)
				{
					graphic.spr_Pointer[1][i].setVisible(true);
				}
				else
				{
					graphic.spr_Pointer[1][i].setVisible(false);
				}
			}
		}
		
		
		super.onUpdate(pSecondsElapsed);
	}
	
	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		// TODO Auto-generated method stub
		
		if(State_Menu_Current == STATE_MENU_STAGE)
		{
			scroll.onTouchEvent(pSceneTouchEvent);
			m_Scroll.onTouchEvent(pSceneTouchEvent);
			m_ClickDetector.onTouchEvent(pSceneTouchEvent);
		}
		return super.onSceneTouchEvent(pScene, pSceneTouchEvent);
	}
	
	@Override
	public void onClick(ClickDetector pClickDetector, int pPointerID,
			float pSceneX, float pSceneY) 
	{
		if(State_Menu_Current == STATE_MENU_STAGE)
		{
				
			for(int i=0; i < graphic.spr_Icon_Stage.length; i++)
			{
				if(Utils.isOnArea(pSceneX, pSceneY, graphic.spr_Icon_Stage[i]))
				{
//					Utils.TRACE("KECEKEL KWE");
					switchState(STATE_MENU_LEVEL);
					graphic.detachSelectStage();
				}
			}
		}
		super.onClick(pClickDetector, pPointerID, pSceneX, pSceneY);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(State_Menu_Current == STATE_MENU_STAGE)
		{
			Utils.TRACE("kEMBALI KE MENU");
			switchState(STATE_MENU_MAIN);
		}
		return super.onKeyDown(keyCode, event);
	}
}
